package com.hkbigdata.transform;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author liuanbo
 * @creat 2024-04-12-10:56
 * @see 2194550857@qq.com
 */
public class Flink04_TransForm_KeyBy_Anonymous {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        KeyedStream<Integer, String> integerStringKeyedStream = env
                .fromElements(1, 2, 3, 4, 5)
/*                .keyBy(new KeySelector<Integer, String>() {
                    @Override
                    public String getKey(Integer value) throws Exception {
                        return value % 2 == 0 ? "偶数" : "奇数";
                    }
                });*/
                .keyBy(data -> data % 2 == 0 ? "偶数" : "奇数");

        integerStringKeyedStream.reduce(new ReduceFunction<Integer>() {
            @Override
            public Integer reduce(Integer value1, Integer value2) throws Exception {
                return value1 + value2;
            }
        }).print();

        env.execute();


    }
}
